<?php

namespace app\modules\admin\controllers;

use app\models\Admin;
use app\models\User;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\Response;

class PermissionsController extends Controller
{
    public function actionIndex()
    {
        $data = Yii::$app->authManager->getPermissions();
        var_dump($data);
        die;
        return $this->render('index', ['list' => $data]);
    }

    public function actionAddPermissions()
    {
        $permission_name = Yii::$app->request->get('name', '');
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $permission_name = trim(Yii::$app->request->post('name', ''));
                $old_name = trim(Yii::$app->request->post('old_name', ''));
                if (mb_strlen($permission_name, 'utf-8') < 1) {
                    throw new Exception('权限名称不能为空');
                }
                $auth = Yii::$app->authManager;
                $permission = $auth->getPermission($old_name);
                $new_permission = $auth->createPermission($permission_name);
                if ($permission) {
                    $auth->update($old_name, $new_permission);
                } else {
                    $auth->add($new_permission);
                }
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }

        return $this->render('add-permission', ['name' => $permission_name]);
    }

    public function actionSet()
    {
        $admin_info = Admin::find()->where(['<>', 'is_admin', 1])->orderBy(['update_time' => SORT_DESC])->asArray()->all();
        $auth = Yii::$app->authManager;
        $roles = $auth->getRoles();
        foreach ($admin_info as &$item) {
            $adminExistRoles = $auth->getRolesByUser($item['id']);
            foreach ($roles as $value) {
                if (isset($adminExistRoles[$value->name])) {
                    $item['is_role'] = 1;
                } else {
                    $item['is_role'] = 0;
                }
            }
        }
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $admin_id = intval(Yii::$app->request->post('admin_id'));
                if (!$admin_id) {
                    throw new Exception('参数有误');
                }
                $admin_info = Admin::find()->where(['id' => $admin_id])->one();
                if (!$admin_info) {
                    throw new Exception('系统出错,请稍后再试');
                }
                var_dump($admin_info);
                die;
                $role = $auth->getRole('中介管理');
                $role_two = $auth->getRole('中介设置');
                $role_three = $auth->getRole('中介删除');
                $auth->assign($role, $admin_id);
                $auth->assign($role_two, $admin_id);
                $auth->assign($role_three, $admin_id);
                //创建在user表中相对的中介信息
                $user_info = new User();
                $user_info->user_name = $admin_info->admin_name;
                $user_info->user_pwd = $admin_info->admin_pwd;
                $user_info->user_ID = 0;
                $user_info->user_phone = '1111';
                $user_info->user_role = 4;
                $user_info->update_time = time();
                $user_info->create_time = time();
                $user_info->save(0);
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }
        return $this->render('set', ['info' => $admin_info]);
    }

    public function actionCreateMediation()
    {
        $admin_id = Yii::$app->request->get('id');
        $admin_info = [];
        if ($admin_id) {
            $admin_info = Admin::find()->where(['id' => $admin_id])->one();
        }

        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $admin_name = trim(Yii::$app->request->post('admin_name'));
                $admin_pwd = trim(Yii::$app->request->post('admin_pwd'));
                $admin_id = intval(Yii::$app->request->post('admin_id'));

                if (mb_strlen($admin_name, 'utf-8') < 2) {
                    throw new Exception('管理员名称不能小于2位');
                }

                if (mb_strlen($admin_pwd, 'utf-8') < 6) {
                    throw new Exception('密码不能小于6位');
                }

                $admin_arr = Admin::find()->where(['id' => $admin_id])->one();

                if ($admin_arr) {
                    $admin_info = $admin_arr;
                } else {
                    $admin_info = new Admin();
                    $admin_info->create_time = time();
                }
                $admin_info->admin_name = $admin_name;
                $admin_info->admin_pwd = md5($admin_pwd);
                $admin_info->update_time = time();
                $admin_info->save(0);
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }
        return $this->render('create_mediation', ['info' => $admin_info]);
    }

    public function actionOps()
    {
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $admin_id = intval(Yii::$app->request->post('admin_id'));
                if (!$admin_id) {
                    throw new Exception('参数有误');
                }
                $admin_info = Admin::find()->where(['id' => $admin_id])->one();

                if (!$admin_info) {
                    throw new Exception('系统出错,请稍后再试试');
                }
                $auth = Yii::$app->authManager;
                $auth->revokeAll($admin_id);
                Admin::deleteAll(['id' => $admin_id]);
                $user_info = User::find()->where(['user_name' => $admin_info->admin_name, 'user_pwd' => $admin_info->admin_pwd])->one();
                $user_info->user_status = 0;
                $user_info->update_time = time();
                $user_info->save(0);
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }
    }
}